﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class category : System.Web.UI.UserControl
    {
        readonly DALGoodsType theGoodsType = new DALGoodsType();
        readonly DALBrand theBrand = new DALBrand();
        readonly DALTerms theTerms = new DALTerms();
        readonly DALPointsArea thePoinstArea = new DALPointsArea();
        tbGoodsType oGoodsType;
        // Fields
    
        readonly BasePage theBasePage = new BasePage();

        private string sBigType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["tid"]))
                {
                    oGoodsType = theGoodsType.GetModel(int.Parse(Request.QueryString["tid"]));
                    if (oGoodsType != null && oGoodsType.typedepth == 1)
                    {
                        return oGoodsType.parentId.ToString();
                    }
                     return Request.QueryString["tid"];
                }
                else
                {
                    return ""; 
                }

                   
              
            }
        }

        private string sSubType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["subid"]))
                {
                    return Request.QueryString["subid"];
                }
                if (!string.IsNullOrEmpty(Request.QueryString["tid"]))
                {
                    oGoodsType = theGoodsType.GetModel(int.Parse(Request.QueryString["tid"]));
                    if (oGoodsType != null && oGoodsType.typedepth == 1)
                    {
                        return oGoodsType.typeId.ToString();
                    }
                }

                return Request.QueryString["subid"];

            }
        }


        // Methods
        protected void Page_Load(object sender, EventArgs e)
        {

                   
        }

        public string GetGoodsTypeHtml()
        {


            string typemsg = "";
            StringBuilder sbHtml = new StringBuilder();
            StringBuilder sbHtmlTable;
            int iRowIndex = 0;
            string sTopName = "tbMore0";
            string sDisplay = "style='display:inline;'";
            int iMaxIndex = 0;
            bool isHave = false;
            iMaxIndex++;
            sbHtmlTable = new StringBuilder();
            sbHtmlTable.AppendFormat("<table class='cartgory_con_current' cellpadding='0' width='100%' cellspacing='0'><tr><td class='con_left_titile'>商品：</td><td class='cartgory_con_left' >");
            DataTable table2;
            if (string.IsNullOrEmpty(sBigType))
            {
               table2 = theGoodsType.GetMainGoodsTypeList().Tables[0];
            }
            else
            {
                table2 = theGoodsType.GetListByParentId(int.Parse(sBigType), 0).Tables[0];
            }
            if (table2.Rows.Count > 0)
            {

                string sCurrentType = "";
                string sTypeName = "全部";
                sbHtmlTable.AppendFormat("<span class='left_title'><a href='{0}'>{1}</a></span></td><td class='right_con'>"
                                           , getUrl("",null,null,null)
                                           , sTypeName
                                            );

                typemsg = "<div  width='100%' style='background-color:#FFF;'>";
                foreach (DataRow row2 in table2.Rows)
                {
                    sCurrentType = "";
                    if (sSubType== row2["typeId"].ToString())
                    {
                        sCurrentType = "CurrentType";
                        isHave = true;
                    }
                    if (iRowIndex > 5)
                    {
                        sTopName = "span0" + iMaxIndex + "_" + iRowIndex.ToString();
                        sDisplay = "style='display:none;'";
                    }

                    typemsg = typemsg + string.Format("<span   id='{0}' {1}  class='{2}'><a href='{3}'>{4}</a></span>"
                                                  , sTopName
                                                   , sDisplay
                                                   , sCurrentType
                                                   , getUrl( row2["typeId"].ToString(),null, null, null)
                                                   , row2["typeName"].ToString()
                                                   );

                    iRowIndex++;

                }
                if (isHave)
                {
                    typemsg = typemsg.Replace("style='display:none;", "style='display:inline;");
                }
                typemsg = typemsg + "</div></td>";
                if (table2.Rows.Count > 5)
                {

                    typemsg = typemsg + ("<span class='td_top_more' ><a onclick='setMore(0," + iMaxIndex + "," + iRowIndex + ")' style='cursor:pointer;'><p id='divViewMore0" + iMaxIndex + "'>更多<font size=1>&nabla;</font></p></a></span></td>");

                }
                else
                {
                    typemsg = typemsg + "<td class='td_more'></td>";
                }
           
                sbHtmlTable.Append(typemsg + "</tr></table>");
            }

            sbHtml.Append(sbHtmlTable);
            return sbHtml.ToString();


        }


        public string GetGoodsBrand()
        {

            string typemsg = "";
            StringBuilder sbHtml = new StringBuilder();
            StringBuilder sbHtmlTable;
            int iRowIndex = 0;
            string sTopName = "tbMore1";
            string sDisplay = "style='display:inline;'";
            int iMaxIndex = 0;
            bool isHave = false;
            iMaxIndex++;
            sbHtmlTable = new StringBuilder();
            sbHtmlTable.AppendFormat("<table class='cartgory_con_current' cellpadding='0' width='100%' cellspacing='0'><tr><td class='con_left_titile'>品牌：</td><td class='cartgory_con_left' >");

            DataTable table2 = theBrand.GetList("brandId in (select brandId from tbGoods where visible=1 ) ").Tables[0];
            if (table2.Rows.Count > 0)
            {

                string sCurrentType = "";
                string sTypeName = "全部";
                sbHtmlTable.AppendFormat("<span class='left_title'><a href='{0}'>{1}</a></span></td><td class='right_con'>"
                                         ,getUrl(null,"",null,null)
                                       ,sTypeName
                                      );

                typemsg = "<div  width='100%' style='background-color:#FFF;'>";
                foreach (DataRow row2 in table2.Rows)
                {
                    sCurrentType = "";
                    if (Request.QueryString["bid"] == row2["brandId"].ToString())
                    {
                        sCurrentType = "CurrentType";
                        isHave = true;
                    }
                    if (iRowIndex > 5)
                    {
                        sTopName = "span1" + iMaxIndex + "_" + iRowIndex.ToString();
                        sDisplay = "style='display:none;'";
                    }

                    typemsg = typemsg + string.Format("<span   id='{0}' {1}  class='{2}'><a href='{3}'>{4}</a></span>"
                                                    , sTopName
                                                   , sDisplay
                                                   , sCurrentType
                                                   , getUrl(null,row2["brandId"].ToString(),null,null)
                                                   , row2["brandName"].ToString()
                                                   );
                    iRowIndex++;

                }
                if (isHave)
                {
                    typemsg = typemsg.Replace("style='display:none;", "style='display:inline;");
                }

                typemsg = typemsg + "</div></td>";
                if (table2.Rows.Count > 5)
                {

                    typemsg = typemsg + ("<td class='td_more'><span class='td_top_more' ><a onclick='setMore(1," + iMaxIndex + "," + iRowIndex + ")' style='cursor:pointer;'><p id='divViewMore1" + iMaxIndex + "'>更多<font size=1>&nabla;</font></p></a></span></td>");

                }
                else
                {
                    typemsg = typemsg + "<td class='td_more'></td>";
                }
           

                sbHtmlTable.Append(typemsg + "</tr></table>");
            }

            sbHtml.Append(sbHtmlTable);
            return sbHtml.ToString();




        }


        public string GetTermsHtml()
        {
          
            string typemsg = "";
            StringBuilder sbHtml = new StringBuilder();
            StringBuilder sbHtmlTable;
            int iRowIndex = 0;
            string sTopName = "tbMore2";
            string sDisplay = "style='display:inline;'";
            int iMaxIndex = 0;
            bool isHave = false;
            iMaxIndex++;
            sbHtmlTable = new StringBuilder();
            sbHtmlTable.AppendFormat("<table class='cartgory_con_current' cellpadding='0' width='100%' cellspacing='0'><tr><td class='con_left_titile'>节气：</td><td class='cartgory_con_left' >");

            DataTable table2 = this.theTerms.GetList("").Tables[0];
            if (table2.Rows.Count > 0)
            {

                string sCurrentType = "";
                string sTypeName = "全部";
                sbHtmlTable.AppendFormat("<span class='left_title'><a href='{0}'>{1}</a></span></td><td class='right_con'>"
                                        , getUrl(null, null,null, "")
                                        , sTypeName
                                       );

                typemsg = "<div  width='100%' style='background-color:#FFF;'>";
                foreach (DataRow row2 in table2.Rows)
                {
                    sCurrentType = "";
                    if (Request.QueryString["terms"] == row2["id"].ToString())
                    {
                        sCurrentType = "CurrentType";
                        isHave = true;
                    }
                    if (iRowIndex > 5)
                    {
                        sTopName = "span2" + iMaxIndex + "_" + iRowIndex.ToString();
                        sDisplay = "style='display:inline;'";
                    }

                    typemsg = typemsg + string.Format("<span   id='{0}' {1}  class='{2}'><a href='{3}'>{4}</a></span>"
                                                   , sTopName
                                                   , sDisplay
                                                   , sCurrentType
                                                   , getUrl(null, null,null, row2["id"].ToString())
                                                   , row2["termsName"].ToString() 
                                                   );
                    iRowIndex++;

                }
                //if (isHave)
                //{
                //    typemsg = typemsg.Replace("style='display:none;", "style='display:inline;");
                //}

                //typemsg = typemsg + "</div></td>";
                //if (table2.Rows.Count > 5)
                //{

                //    typemsg = typemsg + ("<td class='td_more'><span class='td_top_more' ><a onclick='setMore(2," + iMaxIndex + "," + iRowIndex + ")' style='cursor:pointer;'><p id='divViewMore2" + iMaxIndex + "'>更多<font size=1>&nabla;</font></p></a></span></td>");

                //}
                //else
                //{
                    typemsg = typemsg + "<td class='td_more'></td>";
               // }
             
                sbHtmlTable.Append(typemsg + "</tr></table>");
            }
            sbHtml.Append(sbHtmlTable);
            return sbHtml.ToString();


        }



        public string GetPointsHtml()
        {
            string typemsg = "";
            StringBuilder sbHtml = new StringBuilder();
            StringBuilder sbHtmlTable;
            int iRowIndex = 0;
            string sTopName = "tbMore3";
            string sDisplay = "style='display:inline;'";
            int iMaxIndex = 0;
            bool isHave = false;

            iMaxIndex++;
            sbHtmlTable = new StringBuilder();
            sbHtmlTable.AppendFormat("<table class='cartgory_con_current' cellpadding='0' width='100%' cellspacing='0'><tr><td class='con_left_titile'>福点：</td><td class='cartgory_con_left' >");

            DataTable table2 = this.thePoinstArea.GetList("").Tables[0];
            if (table2.Rows.Count > 0)
            {

                string sCurrentType = "";
                string sTypeName = "全部";
                sbHtmlTable.AppendFormat("<span class='left_title'><a href='{0}'>{1}</a></span></td><td class='right_con'>"
                                       , getUrl(null, null, "",null)
                                        , sTypeName
                                       );

           
                typemsg = "<div  width='100%' style='background-color:#FFF;'>";
                foreach (DataRow row2 in table2.Rows)
                {
                    sCurrentType = "";
                    if (Request.QueryString["points"] == row2["id"].ToString())
                    {
                        sCurrentType = "CurrentType";
                        isHave = true;
                    }
                    if (iRowIndex > 5)
                    {
                        sTopName = "span3" + iMaxIndex + "_" + iRowIndex.ToString();
                        sDisplay = "style='display:none;'";
                    }

                    typemsg = typemsg + string.Format("<span   id='{0}' {1}  class='{2}'><a href='{3}'>{4}</a></span>"
                                                 , sTopName
                                                   , sDisplay
                                                   , sCurrentType
                                                   , getUrl(null, null, row2["id"].ToString(), null)
                                                   , "福点" + row2["pointsFrom"].ToString() + "-" + row2["pointsTo"].ToString() 
                                                   );
                    iRowIndex++;
            
                }

                if (isHave)
                {
                    typemsg = typemsg.Replace("style='display:none;", "style='display:inline;");
                }

                typemsg = typemsg + "</div></td>";
                if (table2.Rows.Count > 5)
                {

                    typemsg = typemsg + ("<td class='td_more'><span class='td_top_more' ><a onclick='setMore(3," + iMaxIndex + "," + iRowIndex + ")' style='cursor:pointer;'><p id='divViewMore3" + iMaxIndex + "'>更多<font size=1>&nabla;</font></p></a></span></td>");

                }
                else
                {
                    typemsg = typemsg + "<td class='td_more'></td>";
                }
             

                sbHtmlTable.Append(typemsg + "</tr></table>");
            }

            sbHtml.Append(sbHtmlTable);
            return sbHtml.ToString();

        }

        string getUrl(string subId,string bid,string points,string terms)
        {
            StringBuilder sUrl = new StringBuilder("hotpro.aspx?tid=" + sBigType);
            if (subId != null)
            {
                sUrl.Append("&subid=" + subId);
            }
            else
            {
                if (Request.QueryString["subid"] != null)
                {
                    sUrl.Append("&subid=" + Request.QueryString["subid"]);
                }
            }
            if (bid != null)
            {
                sUrl.Append("&bid=" + bid);

            }
            else
            {
                if (Request.QueryString["bid"] != null)
                {
                    sUrl.Append("&bid=" + Request.QueryString["bid"]);
                }
            }
            if (points != null)
            {
                sUrl.Append("&points=" + points);

            }
            else
            {
                if (Request.QueryString["points"] != null)
                {
                    sUrl.Append("&points=" + Request.QueryString["points"]);
                }
            }
            if (terms != null)
            {
                sUrl.Append("&terms=" + terms);

            }
            else
            {
                if (Request.QueryString["terms"] != null)
                {
                    sUrl.Append("&terms=" + Request.QueryString["terms"]);
                }
            }
            return sUrl.ToString();
        }



    }
}